package leetcode.hot100;

import java.util.Arrays;

public class Solution581 {

    public static void main(String[] args) {
        int[] nums = {2,6,4,8,10,9,15};
        System.out.println(new Solution581().findUnsortedSubarray(nums));
    }

    public int findUnsortedSubarray(int[] nums) {
        //最简单的做法，排完序后统计不在原位的开始和结束
        int[] sorted = nums.clone();
        Arrays.sort(sorted);
        int left = 0, right = nums.length - 1;
        while (left<=right&&sorted[left]==nums[left]) left++;
        while (left<=right&&sorted[right]==nums[right]) right--;
        return right-left+1;
    }
}
